package router

import (
	"io/fs"

	"gitee.com/zengxinqian/hall"
)

// Group returns a new group.
// Path autocorrection, including trailing slashes, is enabled by default.
func (g *Group[Req, Resp]) Group(path string) *Group[Req, Resp] {
	validatePath(path)

	if len(g.prefix) > 0 && path == "/" {
		return g
	}

	return g.router.Group(g.prefix + path)
}

// GET is a shortcut for group.Handle(hall.MethodGet, path, handler)
func (g *Group[Req, Resp]) GET(path string, handler hall.Handler[Req, Resp]) {
	validatePath(path)

	g.router.GET(g.prefix+path, handler)
}

// HEAD is a shortcut for group.Handle(hall.MethodHead, path, handler)
func (g *Group[Req, Resp]) HEAD(path string, handler hall.Handler[Req, Resp]) {
	validatePath(path)

	g.router.HEAD(g.prefix+path, handler)
}

// POST is a shortcut for group.Handle(hall.MethodPost, path, handler)
func (g *Group[Req, Resp]) POST(path string, handler hall.Handler[Req, Resp]) {
	validatePath(path)

	g.router.POST(g.prefix+path, handler)
}

// PUT is a shortcut for group.Handle(hall.MethodPut, path, handler)
func (g *Group[Req, Resp]) PUT(path string, handler hall.Handler[Req, Resp]) {
	validatePath(path)

	g.router.PUT(g.prefix+path, handler)
}

// PATCH is a shortcut for group.Handle(hall.MethodPatch, path, handler)
func (g *Group[Req, Resp]) PATCH(path string, handler hall.Handler[Req, Resp]) {
	validatePath(path)

	g.router.PATCH(g.prefix+path, handler)
}

// DELETE is a shortcut for group.Handle(hall.MethodDelete, path, handler)
func (g *Group[Req, Resp]) DELETE(path string, handler hall.Handler[Req, Resp]) {
	validatePath(path)

	g.router.DELETE(g.prefix+path, handler)
}

// CONNECT is a shortcut for group.Handle(hall.MethodOptions, path, handler)
func (g *Group[Req, Resp]) CONNECT(path string, handler hall.Handler[Req, Resp]) {
	validatePath(path)

	g.router.CONNECT(g.prefix+path, handler)
}

// OPTIONS is a shortcut for group.Handle(hall.MethodOptions, path, handler)
func (g *Group[Req, Resp]) OPTIONS(path string, handler hall.Handler[Req, Resp]) {
	validatePath(path)

	g.router.OPTIONS(g.prefix+path, handler)
}

// TRACE is a shortcut for group.Handle(hall.MethodOptions, path, handler)
func (g *Group[Req, Resp]) TRACE(path string, handler hall.Handler[Req, Resp]) {
	validatePath(path)

	g.router.TRACE(g.prefix+path, handler)
}

// ANY is a shortcut for group.Handle(router.MethodWild, path, handler)
//
// WARNING: Use only for routes where the request method is not important
func (g *Group[Req, Resp]) ANY(path string, handler hall.Handler[Req, Resp]) {
	validatePath(path)

	g.router.ANY(g.prefix+path, handler)
}

func (g *Group[Req, Resp]) GETFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{}) {
	g.GET(uri, g.router.Wrap(handlerFunc, codecs...))
}

func (g *Group[Req, Resp]) POSTFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{}) {
	g.POST(uri, g.router.Wrap(handlerFunc, codecs...))
}

func (g *Group[Req, Resp]) PUTFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{}) {
	g.PUT(uri, g.router.Wrap(handlerFunc, codecs...))
}

func (g *Group[Req, Resp]) DELETEFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{}) {
	g.DELETE(uri, g.router.Wrap(handlerFunc, codecs...))
}

func (g *Group[Req, Resp]) TRACEFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{}) {
	g.TRACE(uri, g.router.Wrap(handlerFunc, codecs...))
}

func (g *Group[Req, Resp]) OPTIONSFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{}) {
	g.OPTIONS(uri, g.router.Wrap(handlerFunc, codecs...))
}

func (g *Group[Req, Resp]) HEADFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{}) {
	g.HEAD(uri, g.router.Wrap(handlerFunc, codecs...))
}

func (g *Group[Req, Resp]) PATCHFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{}) {
	g.PATCH(uri, g.router.Wrap(handlerFunc, codecs...))
}

func (g *Group[Req, Resp]) ANYFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{}) {
	g.ANY(uri, g.router.Wrap(handlerFunc, codecs...))
}

// ServeFiles serves files from the given file system root path.
// The path must end with "/{filepath:*}", files are then served from the local
// path /defined/root/dir/{filepath:*}.
// For example if root is "/etc" and {filepath:*} is "passwd", the local file
// "/etc/passwd" would be served.
// Internally a httpal.FSHandler is used, therefore http.NotFound is used instead
// Use:
//
//	router.ServeFiles("/src/{filepath:*}", "./")
func (g *Group[Req, Resp]) ServeFiles(path string, rootPath string) {
	validatePath(path)

	g.router.ServeFiles(g.prefix+path, rootPath)
}

// ServeFS serves files from the given file system.
// The path must end with "/{filepath:*}", files are then served from the local
// path /defined/root/dir/{filepath:*}.
// For example if root is "/etc" and {filepath:*} is "passwd", the local file
// "/etc/passwd" would be served.
// Internally a httpal.FSHandler is used, therefore http.NotFound is used instead
// Use:
//
//	router.ServeFS("/src/{filepath:*}", myFilesystem)
func (g *Group[Req, Resp]) ServeFS(path string, filesystem fs.FS) {
	validatePath(path)

	g.router.ServeFS(g.prefix+path, filesystem)
}

// ServeFilesCustom serves files from the given file system settings.
// The path must end with "/{filepath:*}", files are then served from the local
// path /defined/root/dir/{filepath:*}.
// For example if root is "/etc" and {filepath:*} is "passwd", the local file
// "/etc/passwd" would be served.
// Internally a httpal.FSHandler is used, therefore http.NotFound is used instead
// of the Router's NotFound handler.
// Use:
//
//	router.ServeFilesCustom("/src/{filepath:*}", *customFS)
func (g *Group[Req, Resp]) ServeFilesCustom(path string, fs fs.FS) {
	validatePath(path)

	g.router.ServeFilesCustom(g.prefix+path, fs)
}

// Handle registers a new request handler with the given path and method.
//
// For GET, POST, PUT, PATCH and DELETE requests the respective shortcut
// functions can be used.
//
// This function is intended for bulk loading and to allow the usage of less
// frequently used, non-standardized or custom methods (e.g. for internal
// communication with a proxy).
func (g *Group[Req, Resp]) Handle(method, path string, handler hall.Handler[Req, Resp]) {
	validatePath(path)

	g.router.Handle(method, g.prefix+path, handler)
}
